<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

defined('IN_ADMIN') or die('Hacking Attempt!');

func::isLogin();
mysql::begin(1);

$sTable = 'payment';

$iPaymentId = (isset($_GET['id']) && is_numeric($_GET['id'])) ? $_GET['id'] : 0;

//store data before deleted
$sSql = 'SELECT customerId, amount FROM payment WHERE paymentId='. $iPaymentId;
$aResult = mysql::querySql($sSql);
$aRow = mysql::fetchSql($aResult);
$aVals = array(
    'customerId' => $aRow['customerId'],
    'amount' => $aRow['amount']
);
$iIdDeletePayment = mysql::deleteSql($sTable, 'paymentId', $iPaymentId);

$aRow = mysql::selectDebitAmount('debit', $aVals['customerId']);
$iTotal = $aRow['amount'] + $aVals['amount'];
$iIdUpdateDebit = mysql::updateSql('debit', array('amount' => $iTotal), 'customerId', $aVals['customerId']);

if($iIdDeletePayment && $iIdUpdateDebit){
    mysql::commit();
    func::redirect(func::makeUrl('payment', 'list'));
}else
    mysql::rollBack();
?>
